package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjOpinion;
import com.moji.entity.MjOrder;
import com.moji.entity.dto.QueryOpinionDto;
import com.moji.mapper.MjOpinionMapper;
import com.moji.service.MjOpinionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 意见反馈表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-28
 */
@Service
public class MjOpinionServiceImpl extends ServiceImpl<MjOpinionMapper, MjOpinion> implements MjOpinionService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjOpinion> queryByPage(QueryOpinionDto dto) {
        LambdaQueryWrapper<MjOpinion> wrapper = new LambdaQueryWrapper<>();
        //创建时间倒叙
        wrapper.orderByDesc(MjOpinion::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjOpinion> mjOpinions = list(wrapper);
        PageInfo<MjOpinion> info = new PageInfo<>(mjOpinions);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjOpinion mjOpinion) {
        String code= idGenerator.idGenerator("YJFK");
        mjOpinion.setCode(code);
        mjOpinion.setCreateId(MoJiCommonUtils.getUserCode());
        mjOpinion.setUpdateId(MoJiCommonUtils.getUserCode());
        mjOpinion.setCreateTime(new Date());
        mjOpinion.setUpdateTime(new Date());
        return this.save(mjOpinion);
    }

    @Override
    public boolean edit(MjOpinion mjOpinion) {
        MjOpinion mjOpinion1=this.getById(mjOpinion.getId());
        BeanUtils.copyProperties(mjOpinion,mjOpinion1);
        mjOpinion1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjOpinion1.setUpdateTime(new Date());
        return this.updateById(mjOpinion);
    }

    @Override
    public MjOpinion detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        MjOpinion mjOpinion=this.getById(id);
        return mjOpinion;
    }
}
